//
//  LLNotificationManager.swift
//  LiLi
//
//  Created by liuzhu on 2019/1/19.
//  Copyright © 2019 liuzhu. All rights reserved.
//

import UIKit
import UserNotifications
import UserNotificationsUI
class LLNotificationManager: NSObject ,UNUserNotificationCenterDelegate{
    
    // 初始化
    static let notifiManager: LLNotificationManager = {
        let n = LLNotificationManager()
        let components = NSDateComponents()

        components.calendar = Calendar(identifier: .chinese)
        return n
    }()
    
    // 获取单例
    class func shareNotificationManager() -> LLNotificationManager {
        return notifiManager
    }
    
    func createNotification(action:LLAction,finish:@escaping(_ action:LLAction?,_ isSuccess:Bool,_ message:String)->()){
        switch action.repeatTag {
        case ActionRepeatTag.REPEAT_NONE:
            let dateArr = action.dateStr?.components(separatedBy: "-")
            let year = Int(dateArr![0])
            let month = Int(dateArr![1])
            let day = Int(dateArr![2])
            let timeArr = action.timeStr?.components(separatedBy: ":")
            let hour = Int(timeArr![0])
            let min = Int(timeArr![1])
            var components = DateComponents();
            components.year = year ?? 3000;
            components.month = month ?? 1;
            components.day = day ?? 1
            components.hour = hour ?? 0
            components.minute = min ?? 0
            let triger = UNCalendarNotificationTrigger.init(dateMatching: components, repeats: false)
            
            let content = UNMutableNotificationContent()
            content.title = action.titleName!
            content.body = action.des!
            content.sound = UNNotificationSound.default
            let request = UNNotificationRequest.init(identifier: action.recordName!, content: content, trigger: triger)
            UNUserNotificationCenter.current().add(request) { (error) in
                if error == nil{
                    finish(action,true,"创建通知成功")
                }
                else{
                    finish(action,false,"创建通知失败")
                }
            }
            break
        case ActionRepeatTag.REPEAT_DAY:
            let timeArr = action.timeStr?.components(separatedBy: ":")
            let hour = Int(timeArr![0])
            let min = Int(timeArr![1])
            var components = DateComponents();
            components.hour = hour ?? 0
            components.minute = min ?? 0
            let triger = UNCalendarNotificationTrigger.init(dateMatching: components, repeats: true)
            let content = UNMutableNotificationContent()
            content.title = action.titleName!
            content.body = action.des!
            content.sound = UNNotificationSound.default
            let request = UNNotificationRequest.init(identifier: action.recordName!, content: content, trigger: triger)
            UNUserNotificationCenter.current().add(request) { (error) in
                if error == nil{
                    finish(action,true,"创建通知成功")
                }
                else{
                    finish(action,false,"创建通知失败")
                }
            }
            break
        case ActionRepeatTag.REPEAT_WEEK:
            let timeArr = action.timeStr?.components(separatedBy: ":")
            let hour = Int(timeArr![0])
            let min = Int(timeArr![1])
            let date = LLDateUtil.shareLLDateUtil().stringConvertDate(string: action.dateStr!, dateFormat: "yyyy-MM-dd")
            let week = LLDateUtil.shareLLDateUtil().getWeekDay(date: date)
            var components = DateComponents();
            components.hour = hour ?? 0
            components.minute = min ?? 0
            components.weekday = week
            let triger = UNCalendarNotificationTrigger.init(dateMatching: components, repeats: true)
            let content = UNMutableNotificationContent()
            content.title = action.titleName!
            content.body = action.des!
            content.sound = UNNotificationSound.default
            let request = UNNotificationRequest.init(identifier: action.recordName!, content: content, trigger: triger)
            UNUserNotificationCenter.current().add(request) { (error) in
                if error == nil{
                    finish(action,true,"创建通知成功")
                }
                else{
                    finish(action,false,"创建通知失败")
                }
            }
            break
        case ActionRepeatTag.REPEAT_MONTH_SUN:
            let timeArr = action.timeStr?.components(separatedBy: ":")
            let hour = Int(timeArr![0])
            let min = Int(timeArr![1])
            let dateArr = action.dateStr?.components(separatedBy: "-")
            let day = Int(dateArr![2])
            var components = DateComponents();
            components.hour = hour ?? 0
            components.minute = min ?? 0
            components.day = day
            let triger = UNCalendarNotificationTrigger.init(dateMatching: components, repeats: true)
            let content = UNMutableNotificationContent()
            content.title = action.titleName!
            content.body = action.des!
            content.sound = UNNotificationSound.default
            let request = UNNotificationRequest.init(identifier: action.recordName!, content: content, trigger: triger)
            UNUserNotificationCenter.current().add(request) { (error) in
                if error == nil{
                    finish(action,true,"创建通知成功")
                }
                else{
                    finish(action,false,"创建通知失败")
                }
            }
            break
        case ActionRepeatTag.REPEAT_MONTH_MOON:
            let timeArr = action.timeStr?.components(separatedBy: ":")
            let hour = Int(timeArr![0])
            let min = Int(timeArr![1])
            let date = LLDateUtil.shareLLDateUtil().stringConvertDate(string: action.dateStr!, dateFormat: "yyyy-MM-dd")
            let calendarChinese:Calendar = Calendar(identifier: .chinese)
            let day = calendarChinese.component(.day, from: date)
            
            var components = DateComponents();
            components.calendar = calendarChinese
            components.hour = hour ?? 0
            components.minute = min ?? 0
            components.day = day
            let triger = UNCalendarNotificationTrigger.init(dateMatching: components, repeats: true)
            let content = UNMutableNotificationContent()
            content.title = action.titleName!
            content.body = action.des!
            content.sound = UNNotificationSound.default
            let request = UNNotificationRequest.init(identifier: action.recordName!, content: content, trigger: triger)
            UNUserNotificationCenter.current().add(request) { (error) in
                if error == nil{
                    finish(action,true,"创建通知成功")
                }
                else{
                    finish(action,false,"创建通知失败")
                }
            }
            break
        case ActionRepeatTag.REPEAT_YEAR_MOON:
            let timeArr = action.timeStr?.components(separatedBy: ":")
            let hour = Int(timeArr![0])
            let min = Int(timeArr![1])
            let date = LLDateUtil.shareLLDateUtil().stringConvertDate(string: action.dateStr!, dateFormat: "yyyy-MM-dd")
            let calendarChinese:Calendar = Calendar(identifier: .chinese)
            let day = calendarChinese.component(.day, from: date)
            let month = calendarChinese.component(.month, from: date)
            var components = DateComponents();
            components.calendar = calendarChinese
            components.hour = hour ?? 0
            components.minute = min ?? 0
            components.day = day
            components.month = month
            let triger = UNCalendarNotificationTrigger.init(dateMatching: components, repeats: true)
            let content = UNMutableNotificationContent()
            content.title = action.titleName!
            content.body = action.des!
            content.sound = UNNotificationSound.default
            let request = UNNotificationRequest.init(identifier: action.recordName!, content: content, trigger: triger)
            UNUserNotificationCenter.current().add(request) { (error) in
                if error == nil{
                    finish(action,true,"创建通知成功")
                }
                else{
                    finish(action,false,"创建通知失败")
                }
            }
            break
        case ActionRepeatTag.REPEAT_YEAR_SUN:
            let timeArr = action.timeStr?.components(separatedBy: ":")
            let hour = Int(timeArr![0])
            let min = Int(timeArr![1])
            let dateArr = action.dateStr?.components(separatedBy: "-")
            let month = Int(dateArr![1])
            let day = Int(dateArr![2])
            var components = DateComponents();
            components.hour = hour ?? 0
            components.minute = min ?? 0
            components.day = day
            components.month = month
            let triger = UNCalendarNotificationTrigger.init(dateMatching: components, repeats: true)
            let content = UNMutableNotificationContent()
            content.title = action.titleName!
            content.body = action.des!
            content.sound = UNNotificationSound.default
            let request = UNNotificationRequest.init(identifier: action.recordName!, content: content, trigger: triger)
            UNUserNotificationCenter.current().add(request) { (error) in
                if error == nil{
                    finish(action,true,"创建通知成功")
                }
                else{
                    finish(action,false,"创建通知失败")
                }
            }
            break
        }
        
    }
    
    func deleteNotification(action:LLAction,finish:@escaping(_ action:LLAction?,_ isSuccess:Bool,_ message:String)->()){
        UNUserNotificationCenter.current().removeDeliveredNotifications(withIdentifiers: [action.recordName!])
        UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: [action.recordName!])
        
    }
    

}
